# coding: utf-8
from util import (
				udpSocketServer, 
				logging,
				sysEncode,
				BUF_SIZE,
				)
import os

def sendData(sock, fileName):
	"""

	发送文件流
	"""
	data, addr = sock.recvfrom(BUF_SIZE)
	logging.info(sysEncode('Received from %s:%s.' % addr))
	
	sock.sendto('<fileName>:%s' % fileName, addr)

	sock.sendto('<fileSize>:%s' % os.path.getsize(fileName), addr)

	with open(fileName, 'rb') as f:
		data = 'start'
		flag = 0
		while data != '':
			f.seek(flag)
			data = f.read(BUF_SIZE)
			sock.sendto(data, addr)
			flag += BUF_SIZE
		sock.sendto('end', addr)

def main():
	"""
	
	接受客户端的请求信息, 并发送文件流
	"""
	sock = udpSocketServer()
	while True:
		sendData(sock, 'hahah.txt')


if __name__ == '__main__':
	main()

	# import os
	# print os.path.getsize('hahah.txt')